package cn.goodym.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.goodym.po.CaseItem;
import cn.goodym.po.CasePort;
import cn.goodym.po.CasesTest;
import cn.goodym.po.Env;
import cn.goodym.po.JsonVar;
import cn.goodym.po.PagingVO;
import cn.goodym.service.CaseItemService;
import cn.goodym.service.CasePortService;
import cn.goodym.service.CasesTestService;
import cn.goodym.service.EnvService;
import cn.goodym.util.GetMethod;
import cn.goodym.util.JsonTools;
import cn.goodym.util.PostMethod;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * 测试用例控制层
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/cases")
public class CasesController {
	@Resource
	private CaseItemService caseItemService;
	@Resource
	private CasePortService casePortService;
	@Resource
	private CasesTestService casesTestService;
	@Resource
	private EnvService envService;

	/**
	 * 用例页面显示
	 * 
	 * @param model
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/showCase")
	public String showCase(Model model, Integer page) throws Exception {
		List<CasesTest> list = null;
		// // 页码对象
		PagingVO pagingVO = new PagingVO();
		// 设置总页数
		pagingVO.setTotalCount(casesTestService.getCount());
		if (page == null || page == 0) {
			pagingVO.setToPageNo(1);
			list = casesTestService.findByPaging(1);
		} else {
			pagingVO.setToPageNo(page);
			list = casesTestService.findByPaging(page);
		}
		model.addAttribute("casesTestList", list);
		model.addAttribute("pagingVO", pagingVO);
		return "cases/showCase";
	}

	/**
	 * 跳转到添加用例信息页面
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/addCase", method = { RequestMethod.GET })
	public String addCaseUI(Model model) throws Exception {
		
		List<Env> envList = envService.findAll();
		model.addAttribute("envList", envList);
		
		List<CasePort> casePortList = casePortService.findAll();
		model.addAttribute("casePortList", casePortList);
		
		List<CaseItem> caseItemList = caseItemService.findAll();
		model.addAttribute("caseItemList",caseItemList);
		
		return "cases/addCase";
	}
	
	

	/**
	 * 添加用例信息处理
	 * 
	 * @param casesTest
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/addCase", method = { RequestMethod.POST })
	public String addCases(CasesTest casesTest, Model model) throws Exception {

		Date date = new Date();
		casesTest.setCreateTime(date);
		Boolean result = casesTestService.save(casesTest);

		if (!result) {
			model.addAttribute("message", "添加失败");
			return "error";
		}
		// 重定向
		return "redirect:/cases/showCase";
	}

	/**
	 * 删除用例
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/removeCase")
	public String removeCase(Integer id) throws Exception {

		if (id == null) {
			return "cases/showCase";
		}
		casesTestService.removeById(id);
		return "redirect:/cases/showCase";
	}

	/**
	 * 修改用例信息页面显示
	 * 
	 * @param id
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/editCase", method = { RequestMethod.GET })
	public String editCaseUI(Integer id, Model model) throws Exception {
		if (id == null) {
			return "redirect:/cases/showCase";
		}
		
		List<Env> envList = envService.findAll();
		model.addAttribute("envList", envList);

		CasesTest casesTest = casesTestService.findById(id);
		List<JsonVar> listVar = new ArrayList<JsonVar>();
		listVar = JSONArray.parseArray(casesTest.getParamters(), JsonVar.class);// 这里的t是Class<T>
		model.addAttribute("listVar", listVar);

		List<CasePort> casePortsList = casePortService.findAll();
		model.addAttribute("casePortsList", casePortsList);
		model.addAttribute("casesTest", casesTest);
		return "cases/editCase";
	}

	/**
	 * 修改用例数据
	 * 
	 * @param casesTest
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/editCase", method = { RequestMethod.POST })
	public String editCase(CasesTest casesTest) throws Exception {
		CasesTest cas = casesTestService.findById(casesTest.getId());

		casesTest.setCreateTime(cas.getCreateTime());
		Date date = new Date();
		casesTest.setUpdateTime(date);
		
		String cp = casesTest.getParamters().replace("\\t", "");
		String cp3 = cp.replace("\\n", "");
		casesTest.setParamters(cp3);
		
		casesTestService.updateById(casesTest);

		return "redirect:/cases/showCase";
	}

	/**
	 * 跳转到复制页面
	 * @param id
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/copyCase", method = { RequestMethod.GET })
	public String copyCaseUI(Integer id, Model model) throws Exception {
		if (id == null) {
			return "redirect:/cases/showCase";
		}
		CasesTest casesTest = casesTestService.findById(id);
		List<JsonVar> listVar = new ArrayList<JsonVar>();
		listVar = JSONArray.parseArray(casesTest.getParamters(), JsonVar.class);// 这里的t是Class<T>
		model.addAttribute("listVar", listVar);

		List<CasePort> casePortsList = casePortService.findAll();
		model.addAttribute("casePortsList", casePortsList);
		model.addAttribute("casesTest", casesTest);
		return "cases/copyCase";
	}
	
	/**
	 * 保存复制信息
	 * @param casesTest
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/copyCase", method = { RequestMethod.POST })
	public String copyCase(CasesTest casesTest,Model model) throws Exception {
		Date date = new Date();
		casesTest.setCreateTime(date);
		casesTest.setId(null);
		Boolean result = casesTestService.save(casesTest);

		if (!result) {
			model.addAttribute("message", "添加失败");
			return "error";
		}
		return "redirect:/cases/showCase";
	}
	
	/**
	 * 查询
	 * @param caseName
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/selectCase", method = { RequestMethod.POST })
	private String selectCase(String caseName, Model model) throws Exception {

		List<CasesTest> casesTestList = casesTestService.findByName(caseName);
		model.addAttribute("casesTestList", casesTestList);
		return "cases/showCase";
	}

	/**
	 * 运行用例
	 * 
	 * @param portId
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/runCase", method = { RequestMethod.POST })
	@ResponseBody
	public String runCase(String portId,String envId, Model model) throws Exception {
		String resultString = "";
		
		Env env = envService.findById(Integer.parseInt(envId));
		CasePort casePort = casePortService.findById(Integer.parseInt(portId));
		String paramters = JsonTools.getUrlParams(casePort.getParamters());
		
		String url = env.getEnvAddress() + casePort.getPorturl();
		
		if (Integer.valueOf(casePort.getType()) == 0) {
			resultString = GetMethod.sendGet(url + paramters,
					"");
		} else if (Integer.valueOf(casePort.getType()) == 1) {
			resultString = PostMethod.sendPost(url,	paramters, "");
		}
		return resultString;
	}
	
	/*
	@RequestMapping(value = "/selectPort", method = { RequestMethod.POST })
	@ResponseBody
	public String selectPort(String itemId, Model model) throws Exception {
		String resultString = "";
		List<CasePort> casePortList = casePortService.findByItemIdList(Integer.parseInt(itemId));
		
		System.out.println(casePortList);
		return resultString;
	}

	@RequestMapping(value = "/selectP", method = { RequestMethod.GET })
	@ResponseBody
	public String selectPort2(String itemId, Model model) throws Exception {
		String resultString = "";
		List<CasePort> casePortList = casePortService.findByItemIdList(Integer.parseInt(itemId));
		
		
		model.addAttribute("casePortList", casePortList);
		System.out.println(casePortList);
		
		String casejson = JSON.toJSONString(casePortList);
		
		return casejson;
	}
*/

}
